﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TSTK03
    {
        #region ==dwKEY===========
        public static readonly DependencyProperty dwKEYProperty = DependencyProperty.Register("dwKEY",
            typeof(DataRowView), typeof(TSTK03),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwKEYChanged));
        public DataRowView dwKEY
        {
            get
            {
                return (DataRowView)GetValue(dwKEYProperty);
            }
            set
            {
                SetValue(dwKEYProperty, value);
            }
        }
        private static void OndwKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSTK03;
            if (src != null)
            {
                src.dwKEY = (DataRowView)e.NewValue;
                src.Load();
            }
        }
        #endregion 
         
        public double YQty
        {
            get
            {
                return Convert.ToDouble(txtYQty.Text.ToString());
            }
            set
            {
                txtYQty.Text = value.ToString();
            }
        }
        public TSTK03()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_STOCK ();
            InitializeComponent(); 
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
        DataRowView LastdwKEY = null;
        public void Load()
        {
            if (LastdwKEY == dwKEY)
                return;
            LastdwKEY = dwKEY;
            if (dwKEY == null)
                return;

            string ART, VAR, FARBE, SPPART, ZWPART, FBPART, LO, LB, MDATE;
            DataTable tb;
            ART = dwKEY["ART"].ToString();
            VAR = dwKEY["VAR"].ToString();
            FARBE = dwKEY["FARBE"].ToString();
            LO = dwKEY["LO"].ToString();
            LB = dwKEY["LB"].ToString();
            SPPART = dwKEY["SPPART"].ToString();
            ZWPART = dwKEY["ZWPART"].ToString();
            FBPART = dwKEY["FBPART"].ToString();
            MDATE = dwKEY["MDATE"].ToString();
           filter = "ART='" + ART + "' AND VAR='" + VAR + "' AND FARBE='" + FARBE + "' AND LO='" + LO + "' AND LB='" + LB + "' AND MDATE='" + MDATE + "'";
            rows =vm.SetData.Tables["ICC_STOCK"].Select(filter);
            if (rows.Length > 0)
            {
                tb = rows.CopyToDataTable();
                tb.TableName=TB;
                if (vm.SetProd.Tables.Contains(TB))
                {
                    vm.SetProd.Tables[TB].Clear();
                }
                vm.SetProd.Merge(tb);
                YQty = 0;
                foreach (DataRow row in tb.Rows)
                {
                    YQty += Convert.ToDouble(row["STOCKQTY"].ToString());
                }
            } 
            if (vwMaster.View != null)
            {
                vwMaster.View.MoveCurrentToFirst();
            }
        } 
    }
}
